﻿namespace EasyIot.WinApp
{
    public class LockSerialPort
    {
        SerialPortHelper serialPortHelper = new SerialPortHelper(); 
        int writeTimeOut = 10000;
        public static byte HEAD1 = (byte)0x57;// W
        public static byte HEAD2 = (byte)0x4B;// K
        public static byte HEAD3 = (byte)0x4C;// L
        public static byte HEAD4 = (byte)0x59;// Y 

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="strPortName">串口名</param>
        /// <returns>true:成功;false:失败</returns>
        public bool OpenSerialPort(string strPortName)
        {
            string err = string.Empty;
            bool ret = serialPortHelper.BeginWork(strPortName, 9600, 0, 8, 1, ref err); 
            return ret;
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public bool CloseSerialPort(ref string err)
        { 
            return serialPortHelper.EndWork(ref err);
        }

        /// <summary>
        /// 检查串口是否已连接
        /// </summary>
        /// <param name="outMsg">错误信息</param>
        /// <returns>true:正常;false:异常</returns>
        public bool CheckConnection(ref string outMsg)
        {
            if (!serialPortHelper.IsPortOpen())
            {
                outMsg = "the connection fai";
                return false;
            }
            return true;
        } 
        

        // 打开指定箱柜
        public bool OpenLock(byte board, byte channel, ref string strMsg)
        {
            if (!CheckConnection(ref strMsg)) return false;

            bool ret = Open(board, channel, ref strMsg);

            return ret;
        } 

        public static byte[] GetProtocol(byte board, byte[] data, byte cmd)
        {
            int bagLen = 8; if (null != data) bagLen = bagLen + data.Length;
            byte[] ret = new byte[bagLen];
            ret[0] = HEAD1;
            ret[1] = HEAD2;
            ret[2] = HEAD3;
            ret[3] = HEAD4;
            ret[4] = (byte)bagLen;
            ret[5] = board;
            ret[6] = cmd;
            byte XORResult = (byte)(ret[0] ^ ret[1] ^ ret[2] ^ ret[3] ^ ret[4] ^ ret[5] ^ ret[6]);
            if (null != data)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    ret[7 + i] = data[i];
                    XORResult = (byte)(XORResult ^ data[i]);
                }
            }
            ret[bagLen - 1] = XORResult;
            return ret;
        }

        public static bool CheckXOR(byte bagLen, byte board, byte cmd, byte[] data, byte check)
        {
            byte XORResult = (byte)(HEAD1 ^ HEAD2 ^ HEAD3 ^ HEAD4 ^ bagLen ^ board ^ cmd);
            for (int i = 0; i < data.Length; i++)
            {
                XORResult = (byte)(XORResult ^ data[i]);
            }
            return XORResult == check;
        }

        public bool Open(byte board, byte channel, ref string strMsg)
        { 
            byte cmd = (byte)0x82;
            byte[] data = new byte[1];
            data[0] = channel;
            byte[] writeBuffer = GetProtocol(board, data, cmd);  
            serialPortHelper.Send(cmd, writeBuffer, writeTimeOut, ref strMsg); 
            return true;
        } 
    }
}